import Schema from "@/uni_modules/uview-ui/libs/util/async-validator";


export let validatorTests = {

}

export let validatorFuncs = {

}

export let validator = (data, rules, {showError=true}={})=>{
  return new Promise((resolve, reject)=>{
    validator = new Schema(rules)

    validator.validate(data, (errors, fields) => {
      if(errors&&errors.length) {
        let ret = ({
          message: errors[0].message,
          errors,
          fields
        })
        if(showError) uni.$x.notify.showToast(ret.message)
        reject(ret)
      }else{
        resolve()
      }
    })
  })
}

export let install = xone=>{
  xone.validator = {
    validatorTests,
    validatorFuncs,
    validator,
  }
}